const app = getApp();
import dayjs from "../../../../../utils/dayjs";
const kugou = require('../../../../../utils/kugouMusic');
const backgroundAudioManager = wx.getBackgroundAudioManager();

// packageMore/pages/music/singer/info/info.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    hide: app.globalData.hide,
    flag: false,
    myInterval: null,
    singerInfo: [],
    singerSongs: [],
    introShowMore: false,
    currentHash: "",
    isPlay: false
  },

  playMusic(e) {
    const that = this;
    const { hash } = e.currentTarget.dataset;
    const { isPlay, currentHash } = that.data;
    if (isPlay && currentHash == hash) {
      app.handleMusic(true, () => {
        that.setData({ isPlay: false });
      });
      return;
    }
    if (!isPlay && currentHash == hash) {
      app.handleMusic(false, () => {
        that.setData({ isPlay: true });
      });
      return;
    }
    kugou.infoMusic(hash).then(result => {
      // console.log(result.data);
      let { error, singerName, songName, album_img, url } = result.data;
      album_img = album_img.replace('{size}/', '');
      if (error != '') {
        wx.showToast({
          title: error,
          icon: 'none'
        })
      } else {
        that.setData({
          currentHash: hash,
          isPlay: true
        });
        app.globalData.isPlay = true;
        app.globalData.currentHash = hash;

        app.setMisic(
          { singerName, songName, album_img, url },
          () => {
            app.globalData.isPlay = false;
            app.globalData.currentHash = '';
            that.setData({ currentHash: '', isPlay: false });
          },
          () => {
            app.globalData.isPlay = true;
            that.setData({ isPlay: true });
          },
          () => {
            app.globalData.isPlay = false;
            that.setData({ isPlay: false });
          }
        );
      }
    }).catch(err => {
      wx.showToast({
        title: err.errMsg,
        icon: 'none'
      })
    })
  },

  introShowMore() {
    let { introShowMore } = this.data;
    if (introShowMore) {
      this.setData({
        introShowMore: false
      })
    } else if (!introShowMore) {
      this.setData({
        introShowMore: true
      })
    }
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    const that = this;
    const { id } = options;
    kugou.getSingerInfo(id).then(res => {
      const { info, songs } = res.data;
      that.setData({
        singerInfo: info,
        singerSongs: songs.list
      })
    }).catch(err => {
      wx.showToast({
        title: err.errMsg,
        icon: 'none'
      })
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    this.setData({
      currentHash: app.globalData.currentHash || "",
      isPlay: app.globalData.isPlay || false
    })
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})